package cn.bnuz.edu.todolist;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bnuz.edu.todolist.database.TodoDB;
import cn.bnuz.edu.todolist.database.TodoDBHelper;
import cn.bnuz.edu.todolist.pojo.Event;

public class TodoListTodayActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todo_list_today_activity);

        List<Event> list = TodoDB.getTodayEvent();
        if (list == null){
            Toast.makeText(this,R.string.list_null_error,Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(TodoListTodayActivity.this,MainActivity.class);
            startActivity(intent);
        }else {
            ListView listView = (ListView)findViewById(R.id.todo_list_today);
            List<Map<String, Object>> listItem = new ArrayList<>();
            for (Event e: list) {
                if (e.getEventType() == 1) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("标题", e.getEventTitle());
                    map.put("时间", e.getEventTime());
                    map.put("状态", e.isCompleted());
                    listItem.add(map);
                }
            }

            SimpleAdapter adapter = new SimpleAdapter(this, listItem, R.layout.todo_list_event_item, new String[]{"标题", "时间"}, new int[]{R.id.event_name, R.id.event_time});
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    Map<String, Object> map = (Map<String, Object>)adapterView.getItemAtPosition(position);
                    CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkbox);
                    Boolean checked = (Boolean) map.get("状态");
                    map.put("状态", !checked);
                    checkBox.setChecked(checked);
                }
            });
        }
    }
}
